package com.csea;

import lombok.AllArgsConstructor;

import java.util.*;

/**
 * 对于简单数据的处理，可以通过外部迭代来操作
 * 如果对于性能有一定的要求，可以使用并行stream来操作
 * 对于复杂对象处理的操作，stream的串行操作和普通的迭代相差无几，甚至超过普通迭代，完全可以简洁的stream来代替普通迭代
 *
 *
 * @author Csea
 * @title
 * @date 2019/12/12 21:24
 */
public class Test3 {

    public static void main(String[] args) {
        Random random = new Random();
        // 复杂数据类型：对象
        List<Product> productList = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            productList.add(new Product("pro" + i, i, random.nextInt(Integer.MAX_VALUE)));
        }

        // 1.stream
        testStream(productList);
        // 2.parallelStream
        testParallelStream(productList);
        // 3.普通for
        testForLoop(productList);
        // 4.增强型for
        testStrongForLoop(productList);
        // 5. 迭代器
        testInterator(productList);
    }

    public static void testStream(List<Product> list) {
        long start = System.currentTimeMillis();
        Optional optional = list.stream().max((p1, p2) -> p1.hot - p2.hot);
        System.out.println(optional.get());
        long end = System.currentTimeMillis();
        System.out.println("testStream：" + (end - start) + "ms");
    }

    public static void testParallelStream(List<Product> list) {
        long start = System.currentTimeMillis();
        Optional optional = list.parallelStream().max((p1, p2) -> p1.hot - p2.hot);
        System.out.println(optional.get());

        long end = System.currentTimeMillis();
        System.out.println("testParallelStream：" + (end - start) + "ms");
    }

    public static void testForLoop(List<Product> list) {
        long start = System.currentTimeMillis();
        Product max = list.get(0);
        for (int i = 1; i < list.size(); i++) {
            if (list.get(i).hot > max.hot) {
                max = list.get(i);
            }
        }
        System.out.println(max);

        long end = System.currentTimeMillis();
        System.out.println("testForLoop：" + (end - start) + "ms");
    }

    public static void testStrongForLoop(List<Product> list) {
        long start = System.currentTimeMillis();
        Product max = list.get(0);
        for (Product product : list) {
            if (product.hot > max.hot) {
                max = product;
            }
        }
        System.out.println(max);
        long end = System.currentTimeMillis();
        System.out.println("testStrongForLoop：" + (end - start) + "ms");
    }

    public static void testInterator(List<Product> list) {
        long start = System.currentTimeMillis();
        Iterator<Product> it = list.iterator();
        Product max = it.next();
        while (it.hasNext()) {
            Product current = it.next();
            if (current.hot > max.hot) {
                max = current;
            }
        }
        System.out.println(max);
        long end = System.currentTimeMillis();
        System.out.println("testInterator：" + (end - start) + "ms");
    }

    @AllArgsConstructor
    static
    class Product {
        String name;
        Integer stock;
        Integer hot;
    }
}
